package net.cmkj.mine.core.admin.sys.user;

import com.jfinal.plugin.activerecord.Page;
import net.cmkj.mine.bean.SysRole;
import net.cmkj.mine.bean.SysUser;
import net.cmkj.mine.common.anno.RequiresPermissions;
import net.cmkj.mine.common.controller.BaseController;
import net.cmkj.mine.common.model.DataGridModel;
import net.cmkj.mine.util.DataGridModelUtils;
import net.cmkj.mine.util.MyDigestUtils;

public class UserController extends BaseController {
	
	private SysUser dao = SysUser.dao;
	
	@RequiresPermissions(value={"/sys/user"}) 
	public void index() {
		render("user.html");
	}
	
	@RequiresPermissions(value={"/sys/user"})
	public void form() throws Exception {
		Long id = getParaToLong("id");
		if(id != null){
			setAttr("sysuser", dao.findUserById(id));
		}
		render("userForm.html");
	}
	
	/**
	 * 获取用户列表
	 * @throws Exception
	 */
	@RequiresPermissions(value={"/sys/user"})
	public void getList() throws Exception {
		String name=getPara("name");
		String username=getPara("username");
		Integer status=getParaToInt("status");
		Page<SysUser> pageInfo = dao.paginate(getPage(), getRows(), name, username, status, getOrderbyStr());
		DataGridModel result = DataGridModelUtils.toDataGridView(pageInfo);
		renderJson(result);
	}
	
	@RequiresPermissions(value={"/sys/user"}) 
	public void useRole() throws Exception {
		Integer id=getParaToInt("id");
		if(id != null){
			setAttr("user", dao.findById(id));
			setAttr("roletree", SysRole.dao.getRoleZtreeViewList(id));
		}
		render("userRole.html");
	}
	
	/**
	 * 保存/修改用户信息
	 * @throws Exception
	 */
	@RequiresPermissions(value={"/sys/user"})
	public void save() throws Exception {
		Integer id = getParaToInt("id");
		//Integer deptid = getParaToInt("deptid");
		String deptids = getPara("deptids");
		String deptnames = getPara("deptnames");
		String name = getPara("name");
		String username = getPara("username");
		String password = getPara("password");
		String phone = getPara("phone");
		String email = getPara("email");
		String des = getPara("des");
		renderJson(dao.save(id, name, username, password, phone, email, deptids, deptnames, des));
	}
	
	/**
	 * 设置数据启用/禁用
	 * @throws Exception
	 */
	@RequiresPermissions(value={"/sys/user"})
	public void visible() throws Exception {
		Integer visible=getParaToInt("visible");
		Integer id=getParaToInt("id");
		renderJson(dao.setVisible(id, visible));
	}
	
	/**
	 * 保存用户角色
	 * @throws Exception
	 */
	@RequiresPermissions(value={"/sys/user"})
	public void saveUserRoles() throws Exception {
		Integer userid=getParaToInt("id");
		String roleids=getPara("roleIds");
		renderJson(dao.changeUserRoles(userid, roleids));
	}
	
	/**
	 * 重置密码
	 */
	@RequiresPermissions(value={"/sys/user"})
	public void forgetPwd() throws Exception {
		Integer id=this.getParaToInt("id");
		renderJson(dao.savePwdUpdate(id, MyDigestUtils.hash(MyDigestUtils.hash("123456"))));
	}
	
	/**
	 * 删除用户
	 * @throws Exception
	 */
	@RequiresPermissions(value={"/sys/user"})
	public void delete() throws Exception {
		renderJson(dao.deleteByIds(getPara("ids")));
	}

	@RequiresPermissions(value={"/sys/user"})
	public void getData() throws Exception {
        Integer moduleid=getParaToInt("moduleid");
        renderJson(dao.getData(moduleid));
    }
	@RequiresPermissions(value={"/sys/user"})
	public void getDataOne() throws Exception {
        Integer userid=getParaToInt("userid");
        renderJson(dao.getDataOne(userid));
    }

	@RequiresPermissions(value={"/sys/user"})
	public void getUserAll() throws Exception {
		renderJson(dao.getUserAll());
	}

}
